/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.appintro.example.ui;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.Text;
import ohos.agp.render.render3d.ViewHolder;
import ohos.app.Context;

import java.util.HashMap;
import java.util.List;

/**
 * list适配
 *
 * @param <T> 泛型
 */
public abstract class EasyProvider<T> extends BaseItemProvider {
    private Context context;
    private List<T> data;
    private int mLayoutId;

    public EasyProvider(Context context, List<T> data, int mLayoutId) {
        this.context = context;
        this.data = data;
        this.mLayoutId = mLayoutId;
    }

    public List<T> getData() {
        return data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public T getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer parent) {
        ViewHolder viewHolder;
        if (convertComponent != null) {
            viewHolder = (ViewHolder) convertComponent.getTag();
        } else {
            convertComponent = LayoutScatter.getInstance(context).parse(mLayoutId, null, false);
            viewHolder = new ViewHolder(convertComponent);
            convertComponent.setTag(viewHolder);
        }
        bind(viewHolder, getItem(position), position);
        return convertComponent;
    }

    /**
     * 绑定item的控件
     *
     * @param holder   holder
     * @param type     泛型
     * @param position position
     */
    protected abstract void bind(ViewHolder holder, T type, int position);

    /**
     * item持有类
     */
    protected static class ViewHolder {
        HashMap<Integer, Component> mViews = new HashMap<>();
        private Component itemView;

        ViewHolder(Component component) {
            this.itemView = component;
        }

        /**
         * 设置控件文字
         *
         * @param viewId 资源Id
         * @param text   text
         * @return ViewHolder
         */
        public ViewHolder setText(int viewId, String text) {
            ((Text) getView(viewId)).setText(text);
            return this;
        }

        /**
         * 获取资源Id控件
         *
         * @param viewId 资源Id
         * @param <E>    控件泛型
         * @return component
         */
        public <E extends Component> E getView(int viewId) {
            E view = (E) mViews.get(viewId);
            if (view == null) {
                view = (E) itemView.findComponentById(viewId);
                mViews.put(viewId, view);
            }
            return view;
        }
    }
}
